package com.woniu.order.controller;


import com.woniu.model.Result;
import com.woniu.order.Param.OrderPayParam;
import com.woniu.order.model.CurriculumOrder;
import com.woniu.order.service.CurriculumOrderService;
import com.woniu.util.JwtUtil;
import com.woniu.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@RestController
@RequestMapping("/curriculumOrder")
//@Api(tags = "团课下单,支付回调接口")
public class CurriculumOrderController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CurriculumOrderService curriculumOrderService;
//    @GetMapping("/orderLeagueClass")
//    @ApiOperation("团课下单接口")
    public Result<?> orderLeagueClass(@RequestHeader String token, Integer groupLessonsClassId) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("userId").toString());
        String ss = curriculumOrderService.orderLeagueClass(userId,groupLessonsClassId);
        return Result.getResult().setData(ss).setMsg("下单成功");
    }

//    @GetMapping("/loadClassOrderByUserId")
//    @ApiOperation("加载用户的订单")
    public Result<List<CurriculumOrder>> loadClassOrderByUserId(@RequestHeader String token, Integer orderState) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("userId").toString());
        List<CurriculumOrder> list = curriculumOrderService.loadClassOrderByUserId(userId,orderState);
        return Result.getListResult(CurriculumOrder.class).setMsg("查询订单成功").setData(list);
    }


//    @GetMapping("/pay")
//    @ApiOperation(value = "发起支付")
    public Result<?> pay(String orderNum, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);//从短token取出键
        int userId = Integer.parseInt(JwtUtil.parseUserToken(refreshToken).get("userId").toString());

        OrderPayParam orderPayParam = new OrderPayParam();
        orderPayParam.setOrderNum(orderNum);
        orderPayParam.setUserId(userId);

        String pay = curriculumOrderService.pay(orderPayParam);

        return Result.getResult().setCode(200).setMsg("发起支付成功").setData(pay);
    }

//    @PostMapping("/notify")
//    @ApiOperation(value = "给支付宝回调的接口")
    public String payNotify(HttpServletRequest request) throws Exception {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = iter.next();
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        curriculumOrderService.payNotify(params);
        return "SUCCESS";
    }


}

